package com.mashacoder.volumecontroller;

import java.lang.reflect.Field;
import java.util.Vector;

import com.mashacoder.volumecontroler.R;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.readystatesoftware.systembartint.SystemBarTintManager.SystemBarConfig;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;

public class MainActivity extends ActionBarActivity {

	private static Vector<CardView> card_list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		/*
		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		*/

		getOverFlowMenu();
		
		
		if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            SystemBarConfig config = tintManager.getConfig();
            
            // order is inportant, set padding before set statusbar color
	        ScrollView scrollView = (ScrollView) this.findViewById(R.id.scrollView);
            scrollView.setPadding(0, config.getPixelInsetTop(true), 0, config.getPixelInsetBottom());
            
			ColorDrawable color = new ColorDrawable(Color.parseColor(
				getResources().getString(R.color.actionbar_color)));
            tintManager.setNavigationBarTintEnabled(true);
            tintManager.setNavigationBarAlpha(0);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintDrawable(color);
		}		
		
		// add card start
        String title;
        LinearLayout main_layout = (LinearLayout)this.findViewById(R.id.main_layout);
        CardView card;
        card_list = new Vector<CardView>();
        
        title = getResources().getString(R.string.volume_media);
        card = new CardView(AudioManager.STREAM_MUSIC, title, 0, this);
//        card = new CardView(AudioManager.STREAM_MUSIC, title, R.drawable.blue1, this);
        main_layout.addView(card.get_view());
        card_list.add(card);
        
        title = getResources().getString(R.string.volume_system);
        card = new CardView(AudioManager.STREAM_SYSTEM, title, 0, this);
        main_layout.addView(card.get_view());
        card_list.add(card);
        
        title = getResources().getString(R.string.volume_ringer);
        card = new CardView(AudioManager.STREAM_RING, title, 0, this);
        main_layout.addView(card.get_view());
        card_list.add(card);
        
        title = getResources().getString(R.string.volume_noti);
        card = new CardView(AudioManager.STREAM_NOTIFICATION, title, 0, this);
        main_layout.addView(card.get_view());
        card_list.add(card);
        
        title = getResources().getString(R.string.volume_alarm);
        card = new CardView(AudioManager.STREAM_ALARM, title, 0, this);
        main_layout.addView(card.get_view());
        card_list.add(card);
        
        title = getResources().getString(R.string.volume_voice);
        card = new CardView(AudioManager.STREAM_VOICE_CALL, title, 0, this);
        main_layout.addView(card.get_view());
        card_list.add(card);
	}
	
	// CardView will call this
	public static int refresh_card() {
		CardView card_view;
		for (int i=0; i < card_list.size(); i++) {
			card_view = card_list.get(i);
			card_view.refresh_sb();
		}
		
		return 0;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}
	 */
	
	// add over flow menu if machine has a real menu button
	public void getOverFlowMenu() {
		try {
			ViewConfiguration config = ViewConfiguration.get(this);
			Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
			if(menuKeyField != null) {
				menuKeyField.setAccessible(true);
				menuKeyField.setBoolean(config, false);
			}
		} catch (Exception ex) {
		// Ignore
		}
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
			refresh_card();
		}
		
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			System.exit(0);
		}
		return super.onKeyDown(keyCode, event);
	}
}
